import activity.*
import fake.Item
import history.Account

class BootStrap {
    def init = { servletContext ->
            return
            try {
                //測試分頁tagLib
                //println 'Create'
                for(i in 1..150) {
                    new Item(name:"Item$i").save()
                }
                //最大權限帳號
                def a0 = new Account(login:"Administrator",pw:"@dmin2014",name:"Administrator",memo:"最大權限").save()
                //測試群組帳號
                def a1 = new Account(login:"a44444",pw:"1234",name:"張無忌",memo:"張無忌").save()
                if(a1.hasErrors()) println a1.errors
                def a2 = new Account(login:"a22222",pw:"1234",name:"謝遜",memo:"謝遜").save()
                if(a2.hasErrors()) println a2.errors
                def t1 = new history.Team(name:"管理者",memo:"管理者").save()
                if(t1.hasErrors()) println t1.errors
                t1.addToAccounts(a1)
                t1.addToAccounts(a2)
                def a3 = new Account(login:"a33333",pw:"1234",name:"黃蓉",memo:"黃蓉").save()
                def t2 = new history.Team(name:"資訊科",memo:"資訊科").save()
                if(t2.hasErrors()) println t2.errors
                t2.addToAccounts(a3)
                //測試權限
                def p1 = new history.Power(url:"account",menu:"權限管理",item:"帳戶管理").save()
                t1.addToPowers(p1)
                if(p1.hasErrors()) println p1.errors
                def p2 = new history.Power(url:"team",menu:"權限管理",item:"群組管理").save()
                t1.addToPowers(p2)
                if(p2.hasErrors()) println p2.errors
                //新增場次類型
                def k1 = new code.Kind(name:'樂齡').save()
                if(k1.hasErrors()) println k1.errors
                def k2 = new code.Kind(name:'好活').save()
                if(k2.hasErrors()) println k2.errors
                //新增場地類型
                def pp1 = new code.Place(name:'一樓兒童室').save()
                if(pp1.hasErrors()) println pp1.errors
                def pp2 = new code.Place(name:'二樓國際會議室').save()
                if(pp2.hasErrors()) println pp2.errors
                //測試活動
                def s1 = new Serial(name:'數位花園', host:'國立公共資訊圖書館').save()
                //serial.addToHistory(new History(type:"Created",isArchive:"N"))
                if(s1.hasErrors()) println s1.errors
                def s2 = new Serial(name:'二個月後系列', host:'國立公共資訊圖書館').save()
                if(s2.hasErrors()) println s2.errors
                //測試場次
                def e1 = new Event(serial:s1,kind:k2,audience:'青少年',name: '數位花園一',capacityQty:1,standbyQty:1,regStart:new Date()-2,regEnd:new Date()+2).save()
                s1.addToEvents(e1)
                e1.updateStatus()
                def e2 = new Event(serial:s1,kind:k2,audience:'青少年',name: '數位花園二',capacityQty:10,standbyQty:5,regStart:new Date()-2,regEnd:new Date()+2).save()
                s1.addToEvents(e2)
                e2.updateStatus()
                def e3 = new Event(serial:s2,kind:k2,audience:'青少年',name: '二個月後活動一',capacityQty:1,
                    standbyQty:0,dateStart:new Date()+55,dateEnd:new Date()+55,regStart:new Date()+50,regEnd:new Date()+64).save()
                s2.addToEvents(e3)
                e3.updateStatus()
                //測試最新消息
                def cont = '<h1>最新消息標題。</h1><br/>最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。最新消息內容。'
                def billboard = new Billboard(title:'最新消息標題一', content:cont, 
                    dateStart: Date.parse("yyyy/M/d",'2014/1/1'), dateEnd:Date.parse("yyyy/M/d",'2014/12/31')).save()
                //測試免審通知信
                def text = '<p>#reader.name#，您好：</p> <p>謝謝您報名「#serial.name#」活動之「#event.name#」場次，將於#event.dateStart#於#event.place#舉行。</p> <p>您的報名系統碼為：#register.sysCode#，取消報名之取消碼為：#register.cancelCode#。</p> <p>國立公共資訊圖書館　敬啟</p>'
                def inform1 = new Inform(name:'正取報名信',active:'Y',subject:'活動報名成功通知信',content:text,timing:'正取報名').save()
                def inform2 = new Inform(name:'候補報名信',active:'Y',subject:'候補報名通知信',content:text,timing:'候補報名').save()
                //println 'Create End'
            } catch (Exception e) {
                println "BootStrap範例資料安裝免除"
            }
            println "BootStrap範例資料安裝完成"
    }
    def destroy = {
    }
}